package pl.msikora.android.lapmagic.view;

import pl.msikora.android.lapmagic.R;
import pl.msikora.android.lapmagic.database.LapMagicDatabaseHelper;
import pl.msikora.android.lapmagic.database.LapsTable;
import pl.msikora.android.lapmagic.database.SessionsTable;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

/**
 * View which represents interactive list of saved sessions.
 * 
 * @author Maciej Sikora
 * 
 */
public class SessionsActivity extends SherlockListActivity {

	private final String TAG = "SessionsActivity";
	private final String BEST_LAP = "best_lap";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clickable_list);
		TextView textOnEmpty = (TextView) findViewById(android.R.id.empty);
		textOnEmpty.setText(R.string.sessions_no_sessions);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setTitle(R.string.sessions_title);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "Filling the list view with data during start");
		fillData();
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "Closing database on destroy");
		LapMagicDatabaseHelper.getInstance(this).close();
		super.onDestroy();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case android.R.id.home:
    		Intent home = new Intent(this, MainActivity.class);
    		home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		startActivity(home);
    		return true;
    	}
    	return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	// TODO call activity that presents session data
    	
    	/*
    	Intent intent = new Intent(this, TrackDetailActivity.class);
    	Log.d(TAG, "list item clicked, track id = " + id);
    	intent.putExtra("id", id);
    	startActivity(intent);
    	*/
    }
	
    private void fillData() {
		SQLiteDatabase db = LapMagicDatabaseHelper.getInstance(this)
				.getReadableDatabase();
				
		Cursor sessionsCursor = db.rawQuery("select " + SessionsTable
			.COLUMN_ID + ", " + SessionsTable.COLUMN_NAME + ", "
			+ SessionsTable.COLUMN_DATE + ", ifnull('" + getString(R.string
			.sessions_best_lap) + " ' || strftime('%M:%f'" + ", min("
			+ LapsTable.COLUMN_LAP_TIME + "/1000.0), 'unixepoch')" + ", '"
			+ getString(R.string.sessions_no_laps) + "') as " + BEST_LAP
			+ " from " + SessionsTable.TABLE_SESSIONS + " left join "
			+ LapsTable.TABLE_LAPS + " on " + SessionsTable.COLUMN_ID + " = "
			+ LapsTable.COLUMN_SESSION_ID + " group by " + SessionsTable
			.COLUMN_ID + ", " + SessionsTable.COLUMN_NAME + ", "
			+ SessionsTable.COLUMN_DATE + " order by " + SessionsTable
			.COLUMN_DATE + " desc", null);
		
		startManagingCursor(sessionsCursor);
		
		String[] from = new String[] { SessionsTable.COLUMN_NAME,
				SessionsTable.COLUMN_DATE , BEST_LAP};
		int[] to = new int[] { R.id.sessionLabel, R.id.sessionDate,
				R.id.sessionBest};
		SimpleCursorAdapter tracks = 
				new SimpleCursorAdapter(this, R.layout.sessions_row,
						sessionsCursor, from, to, 0);
		setListAdapter(tracks);
	}
	
}
